package util;
import java.io.IOException;
import java.io.InputStream;

public class Keyboard
{

    public Keyboard()
    {
    }

    private static String keyboardReadInt()
    {
        int in = 0;
        boolean sinal = false;
        StringBuffer Valor = new StringBuffer("");
        do
        {
            try
            {
                in = System.in.read();
                char chr = (char)in;
                if((in != 10) & (in != 13) && (in >= 48 && in <= 57 || in == 45 && !sinal))
                {
                    Valor.append(chr);
                    sinal = true;
                }
            }
            catch(IOException ioexception) { }
        } while(in != 10);
        return Valor.toString();
    }

    private static String keyboardReadFloat()
    {
        int in = 0;
        boolean ponto = false;
        boolean sinal = false;
        StringBuffer Valor = new StringBuffer("");
        do
        {
            try
            {
                in = System.in.read();
                char chr = (char)in;
                if((in != 10) & (in != 13) && (in >= 48 && in <= 57 || in == 46 || in == 45 && !sinal))
                {
                    if(in == 46)
                    {
                        if(!ponto)
                        {
                            Valor.append(chr);
                            ponto = true;
                        }
                    } else
                    {
                        Valor.append(chr);
                    }
                    sinal = true;
                }
            }
            catch(IOException ioexception) { }
        } while(in != 10);
        return Valor.toString();
    }

    public static int readInt()
    {
        int retorno;
        try
        {
            retorno = Integer.parseInt(keyboardReadInt());
        }
        catch(NumberFormatException e)
        {
            retorno = 0;
        }
        return retorno;
    }

    public static float readFloat()
    {
        float retorno;
        try
        {
            retorno = Float.parseFloat(keyboardReadFloat());
        }
        catch(NumberFormatException e)
        {
            retorno = 0.0F;
        }
        return retorno;
    }

    public static char readChar()
    {
        int in = 0;
        int cont = 0;
        StringBuffer Valor = new StringBuffer("");
        do
        {
            try
            {
                in = System.in.read();
                char chr = (char)in;
                if((in != 10) & (in != 13))
                {
                    if(cont == 0)
                    {
                        Valor.append(chr);
                    }
                    cont++;
                }
            }
            catch(IOException ioexception) { }
        } while(in != 10);
        return Valor.charAt(0);
    }

    public static String readString()
    {
        int in = 0;
        StringBuffer Valor = new StringBuffer("");
        do
        {
            try
            {
                in = System.in.read();
                char chr = (char)in;
                if((in != 10) & (in != 13))
                {
                    Valor.append(chr);
                }
            }
            catch(IOException ioexception) { }
        } while(in != 10);
        return Valor.toString();
    }
}
